<--- %%NOBANNER%% --> nvars.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| Count the total number of variables, integer value;                |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| 1st argument: indata = or data = or just a data name;              |
| 2nd argument: return=T / Output - return the num of variables as   |
|                    an integer;                                     |
|               return=F / LOG - write the num of variables          |
|                    to the log window;                              |
|               return=  / Both - return the number and write to     |
|                    the log window;                                 |
|------------------<-- Start of Files Created-->---------------------|
| example: %nvars(one, return=log);/%nvars(one, return=F);           |
|          %nvars(indata=one, return=output); /                      |
|          %nvars(indata=one, return=T);                             |
|          %nvars(data=two);/%nvars(two);/%nvars(one,return=both);   |
\-------------------<-- End of Files Created-->---------------------*/
%macro nvars(return=F)/parmbuff;
/*--------------------------------------------\
| Author:   Duo Zhou;                         |
| Created:  2-27-2001 10:55pm;                |
| Purpose:  Count the number of variables in a|
|           dataset;                          |
\--------------------------------------------*/
%local _nvarsdataname_ _nvarsnvars_ return;
%if (%index(%upcase(&syspbuff),RETURN=)) OR (%index(%upcase(&syspbuff),RETURN =)) %then %do;
   %let return=%qscan(&syspbuff,2,%str(,())); 
   %let return=%qscan(&return,2,%str(=)); 
%end;
%else %if (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=))) 
          or (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(OUTPUT=))) %then %do;%end;
%else %let return=OUTPUT;

%let _nvarsdataname_=%qscan(&syspbuff,1,%str(,()));
%if (%index(%quote(%upcase(%sysfunc(compress(%quote(&_nvarsdataname_))))),INDATA=)) or 
    (%index(%quote(%upcase(%sysfunc(compress(%quote(&_nvarsdataname_))))),DATA=)) %then %do;
   %let _nvarsdataname_=%qscan(&_nvarsdataname_,2,%str(=));
%end;
%let _nvarsdsid_=%sysfunc(open(&_nvarsdataname_));
%if &_nvarsdsid_ %then %do;
   %let any=%sysfunc (attrn(&_nvarsdsid_, ANY));
   %if any>=1 %then %do;
      %let _nvarsnvars_=%sysfunc(attrn(&_nvarsdsid_,NVARS));
      %let rc=%sysfunc(close(&_nvarsdsid_));
      %if (not (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=F))) and %index(%upcase(&return),LOG)) 
          or (not (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=F))) and %index(%upcase(&return),F)) 
          or (%index(%upcase(&return),BOTH))
         %then %do; %put --> Note: There are &_nvarsnvars_ variables in dataset %data(&_nvarsdataname_).;
      %end;
   %end;
   %else %do;
      %let _nvarsnvars_=0;
      %if (not (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=F))) and %index(%upcase(&return),LOG)) 
          or (not (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=F))) and %index(%upcase(&return),F)) 
          or (%index(%upcase(&return),BOTH))
         %then %do; %put ==> Alert! There is no variable in the data set %data(&_nvarsdataname_).;
      %end;
   %end;
%end;
%else %do;
   %let _nvarsnvars_=.;
   %if (not (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=F))) and %index(%upcase(&return),LOG)) 
       or (not (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=F))) and %index(%upcase(&return),F)) 
       or (%index(%upcase(&return),BOTH))
      %then %do; %put ==> Alert! Open for data set %data(&_nvarsdataname_) failed.;
   %end;
%end;
%if (%index(%upcase(&return),T))
    or (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(OUTPUT=T)))
    or (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=F)))
    or (%index(%upcase(&return),OUTPUT)) or (%index(%upcase(&return),BOTH)) 
   %then %do; &_nvarsnvars_
%end;
%mend nvars;